package org.jeecg.modules.smoke.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.smoke.entity.LwgyJointSmokeConfiguration;
import org.jeecg.modules.smoke.service.ILwgyJointSmokeConfigurationService;
import org.jeecg.modules.water.service.ILwgyRelaxConnectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
* @Description: 设备管理
* @Author: jeecg-boot
* @Date:   2024-06-25
* @Version: V1.0
*/
@Api(tags="烟感设备管理")
@RestController
@RequestMapping("/device/lwgyJointSmokeConfiguration")
@Slf4j
public class LwgyJointSmokeConfigurationController extends JeecgController<LwgyJointSmokeConfiguration, ILwgyJointSmokeConfigurationService> {
   @Autowired
   private ILwgyJointSmokeConfigurationService lwgyJointSmokeConfigurationService;
/*
   @Autowired
   private ILwgyRelaxNbConnectionService lwgyRelaxNbConnectionService;
*/

   @Autowired
   private ILwgyRelaxConnectionService lwgyRelaxConnectionService;
  /* @Autowired
   private ILwgyJointWaterCmdService lwgyJointSmokeCmdService;*/

   /**
    * 分页列表查询
    *
    * @param lwgyJointSmokeConfiguration
    * @param pageNo
    * @param pageSize
    * @param req
    * @return
    */
   @AutoLog(value = "烟感设备管理-分页列表查询")
   @ApiOperation(value="烟感设备管理-分页列表查询", notes="烟感设备管理-分页列表查询")
   @GetMapping(value = "/list")
   public Result<IPage<LwgyJointSmokeConfiguration>> queryPageList(LwgyJointSmokeConfiguration lwgyJointSmokeConfiguration,
                                                                   @RequestParam(name="treeId",required = false) String treeId,
                                  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                  HttpServletRequest req) {
       System.out.println("treeId:"+treeId);
       System.out.println("getRoomId:"+lwgyJointSmokeConfiguration.getRoomId());
       //QueryWrapper<LwgyJointSmokeConfiguration> queryWrapper = QueryGenerator.initQueryWrapper(lwgyJointSmokeConfiguration, req.getParameterMap());
       QueryWrapper<LwgyJointSmokeConfiguration> queryWrapper = new QueryWrapper<>();
       String deviceNumber = lwgyJointSmokeConfiguration.getDeviceNumber();
       if(oConvertUtils.isNotEmpty(deviceNumber)){
           queryWrapper.eq("device_number",deviceNumber);
       }

       if(oConvertUtils.isNotEmpty(treeId)) {
           queryWrapper.and(wrapper -> wrapper.eq("campus_id", treeId)
                   .or().eq("building_id", treeId)
                   .or().eq("unit_id", treeId)
                   .or().eq("layer_id", treeId)
                   .or().eq("room_id", treeId));
       }
       Page<LwgyJointSmokeConfiguration> page = new Page<LwgyJointSmokeConfiguration>(pageNo, pageSize);
       IPage<LwgyJointSmokeConfiguration> pageList = lwgyJointSmokeConfigurationService.querySmokeDeviceList(page, queryWrapper);
       return Result.OK(pageList);
   }


    /**
     * 分页列表查询
     *
     * @param lwgyJointSmokeConfiguration
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "烟感设备管理-分页列表查询(无设备不返回)")
    @ApiOperation(value="烟感设备管理-分页列表查询(无设备不返回)", notes="烟感设备管理-分页列表查询(无设备不返回)")
    @GetMapping(value = "/sblist")
    public Result<IPage<LwgyJointSmokeConfiguration>> querySbPageList(LwgyJointSmokeConfiguration lwgyJointSmokeConfiguration,
                                                                    @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                                    @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                                    HttpServletRequest req) {


        QueryWrapper<LwgyJointSmokeConfiguration> queryWrapper = new QueryWrapper<>();
        String deviceNumber = lwgyJointSmokeConfiguration.getDeviceNumber();
        if(oConvertUtils.isNotEmpty(deviceNumber)){
            queryWrapper.eq("device_number",deviceNumber);
        }
        queryWrapper.isNotNull("device_number");
        queryWrapper.orderByAsc("campus_id");
        queryWrapper.orderByAsc("building_id");
        queryWrapper.orderByAsc("unit_id");
        queryWrapper.orderByAsc("layer_id");
        queryWrapper.orderByAsc("room_id");
        Page<LwgyJointSmokeConfiguration> page = new Page<LwgyJointSmokeConfiguration>(pageNo, pageSize);
        IPage<LwgyJointSmokeConfiguration> pageList = lwgyJointSmokeConfigurationService.querySmokeDeviceList(page, queryWrapper);
        return Result.OK(pageList);
    }

   /**
    *   添加
    *
    * @param lwgyJointSmokeConfiguration
    * @return
    */
   @AutoLog(value = "烟感设备管理-添加")
   @ApiOperation(value="烟感设备管理-添加", notes="烟感设备管理-添加")
   @PostMapping(value = "/add")
   public Result<String> add(@RequestBody LwgyJointSmokeConfiguration lwgyJointSmokeConfiguration) {
       lwgyJointSmokeConfigurationService.save(lwgyJointSmokeConfiguration);
       return Result.OK("添加成功！");
   }

   /**
    *  编辑
    *
    * @param lwgyJointSmokeConfiguration
    * @return
    */
   @AutoLog(value = "烟感设备管理-编辑")
   @ApiOperation(value="烟感设备管理-编辑", notes="烟感设备管理-编辑")
   @RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
   public Result<String> edit(@RequestBody LwgyJointSmokeConfiguration lwgyJointSmokeConfiguration) {
       lwgyJointSmokeConfigurationService.updateById(lwgyJointSmokeConfiguration);
       return Result.OK("编辑成功!");
   }

   /**
    *   通过id删除
    *
    * @param id
    * @return
    */
   @AutoLog(value = "烟感设备管理-通过id删除")
   @ApiOperation(value="烟感设备管理-通过id删除", notes="烟感设备管理-通过id删除")
   @DeleteMapping(value = "/delete")
   public Result<String> delete(@RequestParam(name="id",required=true) String id) {
       lwgyJointSmokeConfigurationService.removeById(id);
       return Result.OK("删除成功!");
   }

   /**
    *  批量删除
    *
    * @param ids
    * @return
    */
   @AutoLog(value = "烟感设备管理-批量删除")
   @ApiOperation(value="烟感设备管理-批量删除", notes="烟感设备管理-批量删除")
   @DeleteMapping(value = "/deleteBatch")
   public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
       this.lwgyJointSmokeConfigurationService.removeByIds(Arrays.asList(ids.split(",")));
       return Result.OK("批量删除成功!");
   }

   /**
    * 通过id查询
    *
    * @param id
    * @return
    */
   @AutoLog(value = "烟感设备管理-通过id查询")
   @ApiOperation(value="烟感设备管理-通过id查询", notes="烟感设备管理-通过id查询")
   @GetMapping(value = "/queryById")
   public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
       LwgyJointSmokeConfiguration lwgyJointSmokeConfiguration = lwgyJointSmokeConfigurationService.getById(id);
       if(lwgyJointSmokeConfiguration==null) {
           return Result.error("未找到对应数据");
       }
       return Result.OK(lwgyJointSmokeConfiguration);
   }

   /**
   * 导出excel
   *
   * @param request
   * @param lwgyJointSmokeConfiguration
   */
   @RequestMapping(value = "/exportXls")
   public ModelAndView exportXls(HttpServletRequest request, LwgyJointSmokeConfiguration lwgyJointSmokeConfiguration) {
       return super.exportXls(request, lwgyJointSmokeConfiguration, LwgyJointSmokeConfiguration.class, "设备管理");
   }

   /**
     * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
   @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
   public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
       return super.importExcel(request, response, LwgyJointSmokeConfiguration.class);
   }


    /**
     * 通过烟感设备号获取设备时间校准信息，安全配置信息
     *
     * @param lwgyJointSmokeConfiguration
     * @return
     */
    @AutoLog(value = "烟感设备管理-通过烟感设备号获取设备安全配置信息")
    @ApiOperation(value="烟感设备管理-通过烟感设备号获取设备安全配置信息", notes="烟感设备管理-通过烟感设备号获取设备安全配置信息")
    @PutMapping(value = "/queryAqByDeviceNum")
    public Result<?> queryAqByDeviceNum( @RequestBody LwgyJointSmokeConfiguration lwgyJointSmokeConfiguration) {
        try {
            if(lwgyJointSmokeConfigurationService.queryAQByDeviceNum(lwgyJointSmokeConfiguration)) {//获取安全设置
                // lwgyJointSmokeConfigurationService.queryTimeByDeviceNum(lwgyJointSmokeConfiguration);//获取时间设置
                //获取设备时间
                return Result.OK(lwgyJointSmokeConfiguration);
            }else{
                return Result.error("获取失败，请联系管理员或者稍后再试");
            }
        }catch (Exception e){
            return Result.error(e+"");
        }
    }

    /**
     * 通过烟感设备号获取设备时间校准信息，安全配置信息
     *
     * @param lwgyJointSmokeConfiguration
     * @return
     */
    @AutoLog(value = "烟感设备管理-通过烟感设备号获取设备时间")
    @ApiOperation(value="烟感设备管理-通过烟感设备号获取设备时间", notes="烟感设备管理-通过烟感设备号获取设备时间")
    @PutMapping(value = "/queryTimeByDeviceNum")
    public Result<?> queryTimeByDeviceNum( @RequestBody LwgyJointSmokeConfiguration lwgyJointSmokeConfiguration) {
        try {
           if( lwgyJointSmokeConfigurationService.queryTimeByDeviceNum(lwgyJointSmokeConfiguration)) {//获取时间设置
               lwgyJointSmokeConfigurationService.updateById(lwgyJointSmokeConfiguration);
               return Result.OK(lwgyJointSmokeConfiguration);
           }else{
               return Result.error("获取失败，请联系管理员或者稍后再试");
           }
        }catch (Exception e){
            return Result.error(e+"");
        }
    }

    /**
     * 通过烟感设备号获取设备时间校准信息，安全配置信息
     *
     * @param lwgyJointSmokeConfiguration
     * @return
     */
    @AutoLog(value = "烟感设备管理-通过烟感设备号校准设备时间为当前时间")
    @ApiOperation(value="烟感设备管理-通过烟感设备号校准设备时间为当前时间", notes="烟感设备管理-通过烟感设备号校准设备时间为当前时间")
    @PutMapping(value = "/setTimeToDeviceNum")
    public Result<?> setTimeToDeviceNum( @RequestBody LwgyJointSmokeConfiguration lwgyJointSmokeConfiguration) {
        try {
            if( lwgyJointSmokeConfigurationService.setTimeToDeviceNum(lwgyJointSmokeConfiguration)){//获取时间设置

                return Result.OK(lwgyJointSmokeConfiguration);
            }else{
              return Result.error("设置失败，请联系管理员或者稍后再试");
            }
        }catch (Exception e){
            return Result.error(e+"");
        }
    }

    /**
     * 通过烟感设备号获取设备时间校准信息，安全配置信息
     *
     * @param lwgyJointSmokeConfiguration
     * @return
     */
    @AutoLog(value = "烟感设备管理-通过烟感设备号更新安全参数")
    @ApiOperation(value="烟感设备管理-通过烟感设备号更新安全参数", notes="烟感设备管理-通过烟感设备号更新安全参数")
    @PutMapping(value = "/editAq")
    public Result<?> editAq( @RequestBody LwgyJointSmokeConfiguration lwgyJointSmokeConfiguration) {
        try {
            if(lwgyJointSmokeConfigurationService.editAq(lwgyJointSmokeConfiguration)){
                lwgyJointSmokeConfigurationService.updateById(lwgyJointSmokeConfiguration);
                return Result.OK(lwgyJointSmokeConfiguration);
            }else{
                return Result.error("设置失败，请联系管理员或者稍后再试");
            }

        }catch (Exception e){
            return Result.error(e+"");
        }
    }
}
